import useUserStore from '@/store/modules/user'

// 定义一个控制按钮权限是否显示的指令
export const hasPerm = {
  mounted(el, binding) {
    const userStore = useUserStore()
    // 判断给指令传递值是否在用户权限里面存在
    const { value } = binding
    console.log(value)
    if (value) {
      // 若不存在则删除此元素
      if (!userStore.userInfo.btns.includes(value)) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    } else {
      throw new Error('need perms! Like v-hasPerm="\'good:list:add\'"')
    }
  },
}

export const color = {
  mounted(el, binding) {
    el.style.color = binding.value
  },
}

export function setupDirective(app) {
  app.directive('hasPerm', hasPerm)
  app.directive('color', color)
}
